﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ToursAgencyDAL;

namespace TourAgency.Admin.Create
{
    public partial class NewTour : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.checkBoxHotelFacilities.DataSource = ToursAgencyDataFuncs.GetAllFacilities();
                this.checkBoxHotelFacilities.DataBind();

                this.dropDownListDestinations.DataSource = ToursAgencyDataFuncs.GetAllDestinations();
                this.dropDownListDestinations.DataBind();

                this.dropDownListTransports.DataSource = ToursAgencyDataFuncs.GetAllTransports();
                this.dropDownListTransports.DataBind();

                this.dropDownListBoards.DataSource = ToursAgencyDataFuncs.GetAllBoards();
                this.dropDownListBoards.DataBind();
            }
        }

        protected void buttonSubmit_Click(object sender, EventArgs e)
        {
            CultureInfo currentCulture = System.Threading.Thread.CurrentThread.CurrentCulture;
            CultureInfo culture = new CultureInfo("en-US");
            System.Threading.Thread.CurrentThread.CurrentCulture = culture;
            Tour newTour = new Tour();
            newTour.DestinationID = int.Parse(this.dropDownListDestinations.SelectedValue);
            newTour.TransportID = int.Parse(this.dropDownListTransports.SelectedValue);
            newTour.BoardID = int.Parse(this.dropDownListBoards.SelectedValue);

            newTour.Description = this.textBoxDescription.Text;
            newTour.StartDate = DateTime.ParseExact(this.textBoxStartDate.Text, "MM-dd-yyyy", culture);
            newTour.EndDate = DateTime.ParseExact(this.textBoxEndDate.Text, "MM-dd-yyyy", culture);
            newTour.Price = decimal.Parse(this.textBoxPrice.Text);

            ToursAgencyDataFuncs.CreateTour(newTour);

            int currentTourID = newTour.ID;
            foreach (ListItem item in this.checkBoxHotelFacilities.Items)
            {
                if (item.Selected)
                {
                    TourHotelFacility th = new TourHotelFacility();
                    th.TourID = currentTourID;
                    th.HoteFacilityID = int.Parse(item.Value);
                    ToursAgencyDataFuncs.CreateTourHotelFacility(th);
                }
            }


            System.Threading.Thread.CurrentThread.CurrentCulture = currentCulture;
        }
    }
}